package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Component
@Service
public class ProductServiceImpl implements IProductService {


    @Autowired
    ProductConverter converter;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    ItemMapper itemMapper;
    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse detailResponse = new ProductDetailResponse();
        try{
            request.requestCheck();


            ProductDetailDto detailDto = itemMapper.getProductDetail(request.getId());
            if (detailDto.getProductImageSmall()!=null){
                detailDto.setProductImageBig(detailDto.getProductImageSmall().get(0));
            }
            detailResponse.setProductDetailDto(detailDto);
            detailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            detailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("Exception:"+e);
            detailResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            detailResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            ExceptionProcessorUtils.wrapperHandlerException(detailResponse,e);
        }
        return detailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();
        try {
            request.requestCheck();
            PageHelper.startPage(request.getPage(),request.getSize());
            Example example = new Example(Item.class);
            if ("1".equals(request.getSort())){
                example.setOrderByClause("price asc");
            }else if ("-1".equals(request.getSort())){
                example.setOrderByClause("price desc" );
            }
            Example.Criteria criteria = example.createCriteria();
            if (request.getPriceGt() != null){
                criteria.andGreaterThanOrEqualTo("price",request.getPriceGt());
            }
            if (request.getPriceLte() != null){
                criteria.andLessThanOrEqualTo("price",request.getPriceLte());
            }
            List<Item> items = itemMapper.selectByExample(example);
            response.setProductDtoList(converter.items2Dto(items));
            PageInfo<Item> pageInfo = new PageInfo<>(items);
            long total = pageInfo.getTotal();
            response.setTotal(total);
        }catch (Exception e){
            log.error("ProductServiceImpl.getAllProduct occur Exception :" + e);
        }
        return response;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        Example example = new Example(Panel.class);
        example.createCriteria().andEqualTo("name","热门推荐");
        List<Panel> panels = panelMapper.selectByExample(example);
        Set<PanelDto> panelDtos = new HashSet<>();

        for (Panel panel : panels) {
            Example example1 = new Example(PanelContent.class);
            example1.createCriteria().andEqualTo("panelId",panel.getId());
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example1);
            List<PanelContentItem> panelContentItems = new ArrayList<>();
            for (PanelContent panelContent : panelContents) {
                Example example2 = new Example(Item.class);
                example2.createCriteria().andEqualTo("id",panelContent.getProductId());
                Item item = itemMapper.selectOneByExample(example2);
                PanelContentItem panelContentItem = new PanelContentItem(panelContent.getId(), panelContent.getPanelId(), panelContent.getType(), panelContent.getProductId(), panelContent.getSortOrder(), panelContent.getFullUrl(), panelContent.getPicUrl(), panelContent.getPicUrl2(), panelContent.getPicUrl3(), panelContent.getCreated(), panelContent.getUpdated(), item.getTitle(), item.getPrice(), item.getSellPoint());
                panelContentItems.add(panelContentItem);
            }
            panel.setPanelContentItems(panelContentItems);
            PanelDto panelDto = contentConverter.panen2Dto(panel);
            panelDtos.add(panelDto);

        }
        RecommendResponse recommendResponse = new RecommendResponse();
        recommendResponse.setPanelContentItemDtos(panelDtos);
        return recommendResponse;
    }
}
